package com.gym.code.config.admin.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.gym.code.config.admin.bean.db.DBBaseMappingInfo;
import com.gym.code.config.admin.dao.IDBMappingDao;

/**
 * 
 * @author xinchun.wang
 * @email: 532002108@qq.com
 * @createTime 2015-4-11 下午4:40:56
 */
@Repository
public class DBMappingDaoImpl implements IDBMappingDao {
	private static final String insertSql = "insert into db_mapping_info(ip,port,db_name,username,password,jdbc_template_name,base_path,package_path,creater) values (?,?,?,?,?,?,?,?,?)";
	private static final String selectSql = "SELECT id, ip,port,db_name,username,password,jdbc_template_name,base_path,package_path,creater FROM db_mapping_info ";
	private static final String updateSql = "update db_mapping_info set ip = ?,port = ?,db_name = ?,username = ?,password = ?,jdbc_template_name = ?,base_path = ?,package_path = ?,creater = ? where id = ?";
	private static final String deleteSql = "DELETE FROM db_mapping_info WHERE id = ?";

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	public int insert(DBBaseMappingInfo bean) {
		Object[] args = new Object[] {bean.getIp(),bean.getPort(),bean.getDbName(),bean.getUsername(),bean.getPassword(),bean.getJdbcTemplateName(),bean.getBasePath(),bean.getPackagePath(),bean.getCreater()};
		return jdbcTemplate.update(insertSql, args);
	}

	public int update(DBBaseMappingInfo bean) {
		Object[] args = new Object[] {bean.getIp(),bean.getPort(),bean.getDbName(),bean.getUsername(),bean.getPassword(),bean.getJdbcTemplateName(),bean.getBasePath(),bean.getPackagePath(),bean.getCreater(),bean.getId()};
		return jdbcTemplate.update(updateSql, args);
	}

	public DBBaseMappingInfo query(long id) {
		List<DBBaseMappingInfo> result = jdbcTemplate.query(selectSql + " where id = ?", new RowMapper<DBBaseMappingInfo>() {
			@Override
			public DBBaseMappingInfo mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				DBBaseMappingInfo bean = new DBBaseMappingInfo();
				bean.setId(rs.getLong("id"));
				bean.setDbName(rs.getString("db_name"));
				bean.setIp(rs.getString("ip"));
				bean.setPort(rs.getInt("port"));
				bean.setUsername(rs.getString("username"));
				bean.setPassword(rs.getString("password"));
				bean.setJdbcTemplateName(rs.getString("jdbc_template_name"));
				bean.setBasePath(rs.getString("base_path"));
				bean.setPackagePath(rs.getString("package_path"));
				bean.setCreater(rs.getString("creater"));
				return bean;
			
			}
		},id);
		if(CollectionUtils.isEmpty(result)){
			return null;
		}
		return result.get(0);
	}

	@Override
	public List<DBBaseMappingInfo> queryAll() {
		List<DBBaseMappingInfo> result = jdbcTemplate.query(selectSql , new RowMapper<DBBaseMappingInfo>() {
			@Override
			public DBBaseMappingInfo mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				DBBaseMappingInfo bean = new DBBaseMappingInfo();
				bean.setId(rs.getLong("id"));
				bean.setDbName(rs.getString("db_name"));
				bean.setIp(rs.getString("ip"));
				bean.setPort(rs.getInt("port"));
				bean.setUsername(rs.getString("username"));
				bean.setPassword(rs.getString("password"));
				bean.setJdbcTemplateName(rs.getString("jdbc_template_name"));
				bean.setBasePath(rs.getString("base_path"));
				bean.setPackagePath(rs.getString("package_path"));
				bean.setCreater(rs.getString("creater"));
				return bean;
			
			}
		});
		if(CollectionUtils.isEmpty(result)){
			return Collections.emptyList();
		}
		return result;
	}

	@Override
	public int delete(long id) {
		Object[] args = new Object[] {id};
		return jdbcTemplate.update(deleteSql, args);
	}

}
